﻿using System;
using System.Collections.Generic;
using WMPLib;

namespace ScottLane.MediaFeeder.ObjectModel
{
    /// <summary>
    /// Represents a Media Player playlist.
    /// </summary>
    /// <remarks>
    /// Attributes: http://msdn.microsoft.com/en-us/library/bb249386(VS.85).aspx
    /// </remarks>
    public class Playlist
    {
        #region Properties

        private string _name;

        /// <summary>
        /// Gets the playlist name.
        /// </summary>
        public string Name
        {
            get { return _name; }
        }

        private PlaylistType _type;

        /// <summary>
        /// Gets the playlist type.
        /// </summary>
        public PlaylistType Type
        {
            get { return _type; }
        }

        /// <summary>
        /// Gets the total song duration.
        /// </summary>
        public TimeSpan Duration
        {
            get
            {
                TimeSpan duration = TimeSpan.Zero;

                foreach (Song song in _songs)
                {
                    duration += song.Duration;
                }

                return duration;
            }
        }

        /// <summary>
        /// Gets the playlist genres.
        /// </summary>
        public List<string> Genres
        {
            get
            {
                List<string> genres = new List<string>();

                foreach (Song song in _songs)
                {
                    if (song.Genre != null && !genres.Contains(song.Genre))
                    {
                        genres.Add(song.Genre);
                    }
                }

                return genres;
            }
        }

        private List<Song> _songs = new List<Song>();

        /// <summary>
        /// Gets the playlist songs.
        /// </summary>
        public List<Song> Songs
        {
            get { return _songs; }
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the Playlist class.
        /// </summary>
        private Playlist()
        {
        }

        /// <summary>
        /// Initializes a new instance of the Playlist class with the specified name.
        /// </summary>
        /// <param name="name">The playlist name.</param>
        public Playlist(string name)
        {
            IWMPPlaylistCollection collection = MediaPlayer.Instance.playlistCollection;
            IWMPPlaylistArray playlists = collection.getByName(name);

            if (playlists.count == 0)
            {
                throw new ArgumentException("The specified playlist does not exist.", "name");
            }

            ParseFromPlaylist(playlists.Item(0));
        }

        /// <summary>
        /// Parses the playlist details from a Media Player playlist.
        /// </summary>
        /// <param name="playlist">The playlist.</param>
        private void ParseFromPlaylist(IWMPPlaylist playlist)
        {
            _name = playlist.name;
            _type = PlaylistType.Auto;

            if (playlist.getItemInfo("PlaylistType") == "wpl")
            {
                _type = PlaylistType.Manual;
            }

            for (int index = 0; index < playlist.count; index++)
            {
                Song song = new Song(playlist.get_Item(index));
                Songs.Add(song);
            }
        }

        /// <summary>
        /// Plays the playlist in Media Center.
        /// </summary>
        public void Play()
        {
        }

        /// <summary>
        /// Checks whether a playlist with the specified name exists in the Media Center library.
        /// </summary>
        /// <param name="name">The playlist name.</param>
        /// <returns>True if the playlist exists, false otherwise.</returns>
        public static bool Exists(string name)
        {
            IWMPPlaylistCollection collection = MediaPlayer.Instance.playlistCollection;
            IWMPPlaylistArray playlists = collection.getByName(name);
            return playlists.count > 0;
        }

        /// <summary>
        /// Searches for playlists with the specified name.
        /// </summary>
        /// <param name="name">The playlist name.</param>
        /// <param name="type">The playlist type.</param>
        /// <returns>The playlists found by the search.</returns>
        public static List<Playlist> Search(string name, PlaylistType type)
        {
            IWMPPlaylistCollection collection = MediaPlayer.Instance.playlistCollection;
            IWMPPlaylistArray playlistArray = null;

            if (name != null)
            {
                playlistArray = collection.getByName(name);
            }
            else
            {
                playlistArray = collection.getAll();
            }

            List<Playlist> playlists = new List<Playlist>();

            for (int index = 0; index < playlistArray.count; index++)
            {
                Playlist playlist = new Playlist();
                playlist.ParseFromPlaylist(playlistArray.Item(index));

                if (playlist.Name != null)
                {
                    playlists.Add(playlist);
                }
            }

            return playlists;
        }

    }
}